package cn.huiyunche.bms.api.rest;

import cn.huiyunche.bms.api.query.BmsReceiptAuditQuery;
import cn.huiyunche.bms.backend.query.BmsReceiptAuditConditions;
import cn.huiyunche.bms.backend.service.AuditService;
import cn.huiyunche.commons.domain.ResultDTOWithPagination;
import cn.huiyunche.commons.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @FileName:
 * @Description: 收款审核
 * @author: Felix
 * @date: 2017/8/15
 */
@Controller
@RequestMapping(value = "/api/audit", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/audit", description = "收款审核")
public class AuditRest {

    @Autowired
    private AuditService auditService;

    private static final Logger LOGGER = LoggerFactory.getLogger(AuditRest.class);

    /**
     *
     * @param query
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "审核接口", notes = "提交审核申请和附件", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultDTOWithPagination<Object> submit(BmsReceiptAuditQuery query) throws Exception {
        LOGGER.info("AuditRest.submit params : {}");
        ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<Object>(true, null, "提交成功");
        try {
            BmsReceiptAuditConditions conditions = new BmsReceiptAuditConditions();
            BeanUtils.copyProperties(query, conditions);
            auditService.submitData(conditions);

        } catch (Exception e) {
            LOGGER.error("AuditRest.submit error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     *

     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateSalesOrderStatus/{salesOrderCode}/{status}", method = RequestMethod.POST)
    @ApiOperation(value = "审核接口", notes = "提交审核申请和附件", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultDTOWithPagination<Object> updateSalesOrderStatus(@ApiParam(value = "订单号", required = true) @PathVariable(value = "salesOrderCode") String salesOrderCode,
                                                                  @ApiParam(value = "状态", required = true) @PathVariable(value = "status") String status) throws Exception {
        LOGGER.info("AuditRest.submit params : {}");
        ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<Object>(true, null, "提交成功");
        try {
            auditService.callSales(salesOrderCode, status);

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("提交失败！");
            LOGGER.error("AuditRest.submit error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

}
